from aliyunsdkcore.client import AcsClient
from aliyunsdksts.request.v20150401.AssumeRoleRequest import AssumeRoleRequest

# 配置信息
access_key_id = "LTAI5tSh9jqVe8WujWoRjKpQ"
access_key_secret = "A90MWwTDSHj0CiDrmlgMKgJ12vRtYR"
region_id = "cn-hangzhou"
role_arn = "acs:ram::1651480074118571:role/aliyunserviceroleforslsaudit"
role_session_name = "oss-bucket-put-object"

# 创建AcsClient实例
client = AcsClient(access_key_id, access_key_secret, region_id)

# 创建AssumeRoleRequest请求对象
request = AssumeRoleRequest()
request.set_RoleArn(role_arn)
request.set_RoleSessionName(role_session_name)
request.set_DurationSeconds(3600)  # 设置令牌有效期（单位：秒）

# 发送请求并获取响应
response = client.do_action_with_exception(request)

# 解析响应获取STS Token信息
import json
response_json = json.loads(response)
sts_access_key_id = response_json["Credentials"]["AccessKeyId"]
sts_access_key_secret = response_json["Credentials"]["AccessKeySecret"]
sts_token = response_json["Credentials"]["SecurityToken"]

print("stsAccessKeyId: ", sts_access_key_id)
print("stsAccessKeySecret: ", sts_access_key_secret)
print("stsToken: ", sts_token)

